//package com.yunhe.device.service.base.impl;
//
//import com.yunhe.device.domain.base.PointMeasureType;
//import com.yunhe.common.exception.ArgumentErrorException;
//import com.yunhe.common.exception.ObjectNotFoundException;
//import com.yunhe.common.exception.UniqueConstraintsException;
//import com.yunhe.device.repository.base.PointMeasureTypeRepository;
//import com.yunhe.device.service.base.PointMeasureTypeService;
//import com.yunhe.common.util.QueryUtil;
//import com.yunhe.common.util.StringUtil;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.Optional;
//
///**
// * 四遥类型Service
// * @desc 四遥类型服务类实现
// * @author liuronglei
// */
//@Service
//public class PointMeasureTypeServiceImpl implements PointMeasureTypeService {
//    @Autowired
//    private PointMeasureTypeRepository pointMeasureTypeRepository;
//
//    /**
//     * 四遥类型对象处理和判断
//     * @param pointMeasureType 四遥类型对象
//     */
//    private void processBeforeSave(PointMeasureType pointMeasureType)
//            throws ArgumentErrorException, UniqueConstraintsException {
//        //判断四遥类型代码、编号、标题是否为空
//        if (pointMeasureType.getCode() == null
//                || StringUtil.isEmpty(pointMeasureType.getName())
//                || StringUtil.isEmpty(pointMeasureType.getTitle())) {
//            throw new ArgumentErrorException("四遥类型代码、编号、标题不能为空");
//        }
//        //判断单位代码是否已存在
//        PointMeasureType typeByCode = this.findByCode(pointMeasureType.getCode());
//        if (typeByCode != null) {
//            if (pointMeasureType.getId() == null || typeByCode.getId().longValue() != pointMeasureType.getId().longValue()) {
//                throw new UniqueConstraintsException("该四遥类型代码已存在");
//            }
//        }
//        //判断单位编号是否已存在
//        PointMeasureType typeByName = this.findByName(pointMeasureType.getName());
//        if (typeByName != null) {
//            if (pointMeasureType.getId() == null || typeByName.getId().longValue() != pointMeasureType.getId().longValue()) {
//                throw new UniqueConstraintsException("该四遥类型编号已存在");
//            }
//        }
//        //如果序号为空，则默认为代码
//        if (pointMeasureType.getSn() == null && pointMeasureType.getCode() != null) {
//            pointMeasureType.setSn(pointMeasureType.getCode());
//        }
//    }
//
//    /**
//     * 新增四遥类型
//     * @param pointMeasureType 四遥类型对象
//     */
//    @Override
//    @Transactional
//    public PointMeasureType createPointMeasureType(PointMeasureType pointMeasureType)
//            throws ArgumentErrorException, UniqueConstraintsException {
//        pointMeasureType.setId(null);
//        processBeforeSave(pointMeasureType);
//        return pointMeasureTypeRepository.save(pointMeasureType);
//    }
//
//    /**
//     * 删除四遥类型
//     * @param pointMeasureTypeId 四遥类型ID
//     */
//    @Override
//    @Transactional
//    public void deletePointMeasureType(Long pointMeasureTypeId) {
//        pointMeasureTypeRepository.deleteById(pointMeasureTypeId);
//    }
//
//    /**
//     * 更新四遥类型（更新全部字段）
//     * @param pointMeasureType 四遥类型对象
//     */
//    @Override
//    @Transactional
//    public PointMeasureType updatePointMeasureType(PointMeasureType pointMeasureType)
//            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
//        boolean exists = pointMeasureType.getId() != null && pointMeasureTypeRepository.existsById(pointMeasureType.getId());
//        if (!exists) {
//            throw new ObjectNotFoundException("找不到ID为" + pointMeasureType.getId() + "的四遥类型");
//        }
//        processBeforeSave(pointMeasureType);
//        return pointMeasureTypeRepository.save(pointMeasureType);
//    }
//
//    /**
//     * 更新四遥类型（更新指定字段）
//     * @param pointMeasureTypeId 四遥类型ID
//     * @param code 代码
//     * @param name 编号
//     * @param title 标题
//     * @param sn 排序序号
//     * @param activity 是否可用
//     */
//    @Override
//    @Transactional
//    public PointMeasureType updatePointMeasureType(Long pointMeasureTypeId,
//                                                   Long code,
//                                                   String name,
//                                                   String title,
//                                                   Long sn,
//                                                   Boolean activity)
//            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
//        PointMeasureType pointMeasureType = this.findByPointMeasureTypeId(pointMeasureTypeId);
//        if (pointMeasureType == null) {
//            throw new ObjectNotFoundException("找不到ID为" + pointMeasureTypeId + "的四遥类型");
//        }
//        Optional.ofNullable(code).ifPresent(pointMeasureType::setCode);
//        Optional.ofNullable(name).ifPresent(pointMeasureType::setName);
//        Optional.ofNullable(title).ifPresent(pointMeasureType::setTitle);
//        Optional.ofNullable(sn).ifPresent(pointMeasureType::setSn);
//        Optional.ofNullable(activity).ifPresent(pointMeasureType::setActivity);
//        processBeforeSave(pointMeasureType);
//        return pointMeasureTypeRepository.save(pointMeasureType);
//    }
//
//    /**
//     * 根据ID获得四遥类型对象
//     * @param id 状态ID
//     */
//    @Override
//    public PointMeasureType findByPointMeasureTypeId(Long id) {
//        return pointMeasureTypeRepository.findById(id).orElse(null);
//    }
//
//    /**
//     * 根据代码获得四遥类型对象
//     * @param code 代码
//     */
//    @Override
//    public PointMeasureType findByCode(Long code) {
//        return pointMeasureTypeRepository.findByCode(code);
//    }
//
//    /**
//     * 根据编号获得四遥类型对象
//     * @param name 编号
//     */
//    @Override
//    public PointMeasureType findByName(String name) {
//        return pointMeasureTypeRepository.findByName(name);
//    }
//
//    /**
//     * 获得全部四遥类型对象
//     */
//    @Override
//    public Iterable<PointMeasureType> findPointMeasureTypes() {
//        return pointMeasureTypeRepository.findAll(QueryUtil.defaultSort());
//    }
//
//}
